package com.baor.web.controller.mall;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baor.mall.domain.TLevel;
import com.baor.mall.service.TLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.baor.common.annotation.Log;
import com.baor.common.core.controller.BaseController;
import com.baor.common.core.domain.AjaxResult;
import com.baor.common.enums.BusinessType;
import com.baor.mall.domain.TTaxonomic;
import com.baor.mall.service.ITTaxonomicService;
import com.baor.common.utils.poi.ExcelUtil;
import com.baor.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 */
@RestController
@RequestMapping("/mall/taxonomic")
public class TTaxonomicController extends BaseController
{
    @Autowired
    private ITTaxonomicService tTaxonomicService;

    @Autowired
    private TLevelService tLevelService;

    /**
     * 查询商品分类树形结构（用于前端树形展示）
     */
    @PreAuthorize("@ss.hasPermi('mall:taxonomic:list')")
    @GetMapping("/tree")
    public AjaxResult tree(TTaxonomic tTaxonomic) {
        List<TTaxonomic> list = tTaxonomicService.selectTTaxonomicTree(tTaxonomic);
        return success(list);
    }

    /**
     * 查询一级分类列表（供二级分类选择父分类）
     */
    @GetMapping("/parentList")
    public AjaxResult getParentTaxList() {
        List<TTaxonomic> list = tTaxonomicService.selectParentTaxList();
        return success(list);
    }

    /**
     * 原有列表查询（保持不变）
     */
    @PreAuthorize("@ss.hasPermi('mall:taxonomic:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTaxonomic tTaxonomic) {
        startPage();
        List<TTaxonomic> list = tTaxonomicService.selectTTaxonomicList(tTaxonomic);
        return getDataTable(list);
    }

    /**
     * 导出功能（保持不变）
     */
    @PreAuthorize("@ss.hasPermi('mall:taxonomic:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTaxonomic tTaxonomic)
    {
        List<TTaxonomic> list = tTaxonomicService.selectTTaxonomicList(tTaxonomic);
        ExcelUtil<TTaxonomic> util = new ExcelUtil<TTaxonomic>(TTaxonomic.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 以下接口保持不变，仅确保参数正确传递parentId
     */
    @PreAuthorize("@ss.hasPermi('mall:taxonomic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tTaxonomicService.selectTTaxonomicById(id));
    }

    @PreAuthorize("@ss.hasPermi('mall:taxonomic:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTaxonomic tTaxonomic)
    {
        return toAjax(tTaxonomicService.insertTTaxonomic(tTaxonomic));
    }

    @PreAuthorize("@ss.hasPermi('mall:taxonomic:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTaxonomic tTaxonomic)
    {
        return toAjax(tTaxonomicService.updateTTaxonomic(tTaxonomic));
    }

    @PreAuthorize("@ss.hasPermi('mall:taxonomic:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tTaxonomicService.deleteTTaxonomicByIds(ids));
    }

    /**
     * 获取所有级别类型列表（用于前端下拉选择）
     */
    @GetMapping("/levelList")
    public AjaxResult getLevelList() {
        List<TLevel> list = tLevelService.selectTLevelAll();
        return success(list);
    }
   
}